﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using famails.common;
using famails.dto;
using famails.App_Code.source.dto;

namespace famails.dao
{
    /// <summary>
    /// Summary description for GroupDTO
    /// </summary>
    public class CM_GroupDAO
    {
        public CM_GroupDAO()
        {

        }
        public DataTable GetByID(int ID, int UserID)
        {
            SqlCommand cmd = new SqlCommand("[SP_CM_Group_Get4Edit] "+ID+","+UserID, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetAll(int userid)
        {
            SqlCommand cmd = new SqlCommand("SELECT a.*, subName UserName, subEmail FROM tblCM_Groups a left join tblsubclient s on s.subId = a.userid WHERE a.isdelete <> 1 and createby="+userid, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetAll2Customer(int userid)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Group_GetAll2Customer " + userid, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
        public DataTable GetAll2Order(int userid)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Group_GetAll2Order " + userid, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public bool Insert(CM_GroupDTO dto)
        {
            SqlCommand cmd = new SqlCommand("insert into tblCM_Groups(GroupName, UserID, CreateDate, createBy)values(@name, @userid, getdate(), @createby)", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = dto.UserID;
            cmd.Parameters.Add("@CreateBy", SqlDbType.Int).Value = dto.CreateBy;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.GroupName;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        public bool Edit(CM_GroupDTO dto)
        {
            SqlCommand cmd = new SqlCommand("update tblCM_Groups set GroupName=@name, UserID =@UserID where Groupid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.GroupName;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = dto.GroupID;
            cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = dto.UserID;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        public bool Delete(int id)
        {
            SqlCommand cmd = new SqlCommand("update tblCM_Groups set isdelete=1 where Groupid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        internal bool CheckExistsName(string name, int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblCM_Groups WHERE Groupid <> @id and Groupname = @name and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count > 0;
        }

        internal bool CheckBeforeDelete(int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT groupid FROM tblCM_Customers WHERE Groupid = @id and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count == 0;
        }
    }
}
